<template>
  <el-dialog title="设置分类" :visible.sync="dialogFormVisible" width="500px" @close="close" :close-on-click-modal="false" v-dialogDrag>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px" style="height: 100px">
      <el-form-item label="分类" prop="categoryId">
        <treeselect
          v-model="form.categoryId"
          :options="options"
          :disable-branch-nodes="true"
          :show-count="true"
          :normalizer="normalizer"
          :flatten-search-results="false"
          noOptionsText="无分类可选"
          placeholder="请选择分类"
          :max-height="150"
          @select="handleSelect"
        />
      </el-form-item>
      <el-form-item label="是否首选" prop="defValue">
        <el-radio v-model="form.defValue" label="1">是</el-radio>
        <el-radio v-model="form.defValue" label="0">否</el-radio>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="save">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { getList, doAddModelCategory, doEditModelCategory } from '@/api/workflow/modelCategory'

  import Treeselect from '@riophae/vue-treeselect'
  import { handleTree } from '@/utils/index'

  export default {
    name: 'ModelCategory',
    components: { Treeselect },
    data() {
      return {
        form: {
          modelId: undefined,
          categoryId: undefined,
          defValue: '1',
        },
        rules: {
          categoryId: [{ required: true, trigger: ['blur', 'change'], message: '请选择分类' }],
          defValue: [{ required: true, trigger: ['blur', 'change'], message: '请选择' }],
        },
        dialogFormVisible: false,
        options: [],
      }
    },
    created() {},
    methods: {
      handleSelect(node, instanceId) {
        this.form.categoryId = node.categoryId
        this.form.categoryName = node.categoryName
        this.form.categoryCode = node.categoryCode
      },
      async fetchData() {
        await getList().then((res) => {
          if (res.success) {
            this.options = handleTree(res.data, 'categoryId', 'parentId', 'children', 0)
          }
        })
      },
      normalizer(node) {
        if (node.children && !node.children.length) {
          delete node.children
        }
        return {
          id: node.categoryId,
          label: node.categoryName,
          children: node.children,
        }
      },
      showEdit(row) {
        this.form.modelId = row.id
        this.form.categoryId = row.categoryId
        this.form.mcId = row.mcId
        this.form.defValue = row.defValue || '1'
        this.fetchData()
        this.dialogFormVisible = true
      },
      close() {
        this.$refs['form'].resetFields()
        this.form = this.$options.data().form
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            if (this.form.mcId) {
              await doEditModelCategory(this.form).then((res) => this.callbackFun(res))
            } else {
              await doAddModelCategory(this.form).then((res) => this.callbackFun(res))
            }
          } else {
            return false
          }
        })
      },
      callbackFun(res) {
        if (res.success) {
          this.$baseMessage(res.msg, 'success')
          this.$emit('fetch-data')
          this.close()
        } else {
          this.$baseMessage(res.msg, 'error')
        }
      },
    },
  }
</script>
